package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.message.WmNewsMessageConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class ArticleDownOrUp {
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receviceMessage(String message) {
        log.info("接收到执行文章上下架的消息=======>");
        if (StringUtils.isNotBlank(message)) {
            Map map = JSON.parseObject(message, Map.class);
            Object enable = map.get("enable");
            Boolean res = enable.equals(1) ? false : true;
            Integer articleId = (Integer) map.get("articleId");
            ResponseResult result = apArticleConfigService.downOrUpArticle(articleId, res);
            if (result.getCode() != 0) {
                log.info("修改文章上下架失败，失败原因：{}", result.getErrorMessage());
                CustomException.cust(AppHttpCodeEnum.SERVER_ERROR);
            }
            log.info("修改文章上下架状态成功=====");

        }
    }
}
